<?php
namespace Admin\Controller;
class AllRecordController extends ComController{
	//列表页
	/*  
	 * 从驾校表中取驾校id,根据驾校ID 及时间 查找
	 * 
	 * */
	public function index(){
		//从前台接收月份
		$dateS = I('get.dateS')?I('get.dateS'):date('Y-m-d');
		$dateS = strtotime($dateS);
		$dateE = I('get.dateE')?I('get.dateE'):date('Y-m-d',strtotime('+1 month'));
		$dateE = strtotime($dateE);
		$sc_name = I('get.scname','','strip_tags');
		//类别 1正常  2打印
		$type =(int)I('get.type')?I('get.type'):1;
		$p= intval($_GET['p'])?$_GET['p']:1;
		$pagesize=5;
		$offset = $pagesize * ($p - 1);//计算记录偏移量
		
		$school = M('member');
		
		if($type==2){
			//打印
			if(empty($sc_name)){
				//未指定驾校名称,从驾校表中去驾校
				$where['uid']=array('neq',1);
				$count = $school->where($where)->count();
				$sc_info = $school
				->field('uid,user,name,phone')
				->where($where)
				->select();
			}else{
				//指定驾校名称
				$where['name'] =array('like',"%$sc_name%");
				$count = $school->where($where)->count();
				$sc_info = $school
				->field('uid,user,name,phone')
				->where($where)
				->select();
			}	
		}else{
// 			正常显示
			if(empty($sc_name)){
				//未指定驾校名称,从驾校表中去驾校
				$where['uid']=array('neq',1);
				$count = $school->where($where)->count();
				$sc_info = $school
				->field('uid,user,name,phone')
				->where($where)
				->limit($offset . ',' . $pagesize)
				->select();
			}else{
				//指定驾校名称
				$where['name'] =array('like',"%$sc_name%");
				$count = $school->where($where)->count();
				$sc_info = $school
				->field('uid,user,name,phone')
				->where($where)
				->limit($offset . ',' . $pagesize)
				->select();
			}	
		}
		
		//将驾校id取出，组合成字符串
		$sc_id =array();
		if(!empty($sc_info)){
			foreach ($sc_info as $k=>$v){
				$sc_id[] =$v['uid'];
			}
		}else{
			//未查询到驾校信息
			$this->error('未查询到驾校信息');
		}

		//根据驾校ID字符串，查找预约学车
		$meetingP = $this->meetingPrice($sc_id,$dateS,$dateE);

		//根据驾校ID字符串，查找在线报名
		$signP = $this->signPrice($sc_id,$dateS,$dateE);

		foreach ($sc_info as $k=>$v){
			$sc_info[$k]['mTotalP']=$meetingP[$v['uid']]['mTotalP'];
			$sc_info[$k]['sTotalP']=$signP[$v['uid']]['sTotalP'];
			$sc_info[$k]['total']=$signP[$v['uid']]['sTotalP']+$meetingP[$v['uid']]['mTotalP'];
		}

		//分页
		$page = new \Think\Page($count, $pagesize);
		$page = $page->show();
		$this->assign('dateS',$dateS);
		$this->assign('dateE',$dateE);
		$this->assign('info',$sc_info);
		$this->assign('page',$page);
		if($type==2){
			$this->display('index_pri');
		}else{
			$this->display();
		}
		
	}
	
	//在线报名
	public function signPrice($sc_id,$dateS,$dateE){
		//$sc_id = array(24,30);
		$where['d_id']=array('in',$sc_id);
// 		$where['DATE_FORMAT(FROM_UNIXTIME(add_time),\'%Y-%m\')']= $date;
		$where['add_time']=array('between',array($dateS,$dateE));
		//已支付
		$where['ispaid']=1;
		//支付类型 线上支付 1
		$where['pay_way']=1;
		$sign_record = M('sign_record');
		
		$record_info = $sign_record
					->field('id,d_id,money')
					->where($where)
					->select();
		//dump($record_info);
		$totalPrice=array();
		if(!empty($record_info)){
			foreach ($sc_id as $k=>$v){
				foreach ($record_info as $key=>$val){
					if($v == $val['d_id']){
						$totalPrice[$v]['sTotalP']+=$val['money'];
					}
				}
			}
		}
		return $totalPrice;
	}
	
	//预约学车
	public function meetingPrice($sc_id,$dateS,$dateE){
		//$sc_id = array(24,30,19);
		$where['school_id']=array('in',$sc_id);
// 		$where['DATE_FORMAT(FROM_UNIXTIME(add_time),\'%Y-%m\')']= $date;
		$where['add_time']=array('between',array($dateS,$dateE));
		//已支付
		$where['ispay']=1;
		//支付类型为线上支付
		$where['pay_type']=1;
		$car_record = M('meeting_car_record');
		
		$record_info = $car_record
					->field('id,school_id,price')
					->where($where)
					->select();
		$totalPrice=array();
		if(!empty($record_info)){
			foreach ($sc_id as $k=>$v){
				foreach ($record_info as $key=>$val){
					if($v == $val['school_id']){
						$totalPrice[$v]['mTotalP']+=$val['price'];
					}
				}
			}
		}
		return $totalPrice;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}